package jp.oesf.rss;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class DataBase {
	private Context context;

	public DataBase(Context context) {
		this.context = context;
	}

	public boolean registerDataBase(ArrayList<XmlRowModel> arrayList) {
		LexusSQLiteOpenHelper databaseOpenHelper = new LexusSQLiteOpenHelper(
				this.context);
		SQLiteDatabase database = null;

		// 書き込み用SQLiteDatabaseオブジェクトの取得
		database = databaseOpenHelper.getWritableDatabase();

		// 現在のデータの削除
		database.delete("LEXUS_TABLE", null, null);

		int arrayListSize = arrayList.size();

		for (int i = 0; i < arrayListSize; i++) {
			// データの取得
			XmlRowModel rmlRowModel = arrayList.get(i);

			// データの設定
			ContentValues values = new ContentValues();
			values.put("title", rmlRowModel.getTitle());
			values.put("link", rmlRowModel.getLink());
			values.put("description", rmlRowModel.getDescription());
			values.put("pubdate", rmlRowModel.getPubDate());
			values.put("guid", rmlRowModel.getGuid());
			values.put("category", rmlRowModel.getCategory());

			// データの登録
			long res = database.insert("LEXUS_TABLE", null, values);

			// データの登録失敗
			if (res == -1) {
				// データベースの切断
				databaseOpenHelper.close();
				return false;
			}
		}

		// データベースの切断
		databaseOpenHelper.close();
		return true;
	}

	public void searchAllDataBase(ListActivity listview) {

		LexusSQLiteOpenHelper databaseOpenHelper = new LexusSQLiteOpenHelper(
				this.context);
		SQLiteDatabase database = null;

		// 読み込み用SQLiteDatabaseの取得
		database = databaseOpenHelper.getReadableDatabase();

		// 全件検索
		Cursor cursor = database.query("LEXUS_TABLE", null, null, null, null,
				null, null);

		if (cursor != null) {
			listview.startManagingCursor(cursor);

			// SimpleCursorAdapterの生成
			SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(
					this.context, R.layout.list_row, cursor,
					new String[] { "title" }, new int[] { R.id.title });

			// ListActivityの登録
			listview.setListAdapter(cursorAdapter);
		}

		// データベースの切断
		databaseOpenHelper.close();
	}

	public void advancedSearchDataBase(Activity detailview) {

		Bundle extras = detailview.getIntent().getExtras();
		long id = extras.getLong("id");

		LexusSQLiteOpenHelper databaseOpenHelper = new LexusSQLiteOpenHelper(
				this.context);

		SQLiteDatabase database = null;

		// 読み込み用SQLiteDatabaseの取得
		database = databaseOpenHelper.getReadableDatabase();

		// 条件検索
		Cursor cursor = database.query("LEXUS_TABLE", null, "_id=" + id, null,
				null, null, null);

		if (cursor != null) {
			detailview.startManagingCursor(cursor);
			cursor.moveToFirst();

			// titleの設定
			((TextView) detailview.findViewById(R.id.text_title))
					.setText(cursor.getString(cursor.getColumnIndex("title")));

			// linkの設定
			((TextView) detailview.findViewById(R.id.text_link)).setText(cursor
					.getString(cursor.getColumnIndex("link")));

			// descriptionの設定
			((TextView) detailview.findViewById(R.id.text_description))
					.setText(cursor.getString(cursor
							.getColumnIndex("description")));
			// pubdateの設定
			((TextView) detailview.findViewById(R.id.text_pubdate))
					.setText(cursor.getString(cursor.getColumnIndex("pubdate")));

			// guidの設定
			((TextView) detailview.findViewById(R.id.text_guid)).setText(cursor
					.getString(cursor.getColumnIndex("guid")));

			// categoryの設定
			((TextView) detailview.findViewById(R.id.text_category))
					.setText(cursor
							.getString(cursor.getColumnIndex("category")));
		}

		// データベースの切断
		databaseOpenHelper.close();
	}
}